<?php
/**
 *  This file is part of amfPHP
 *
 * LICENSE
 *
 * This source file is subject to the license that is bundled
 * with this package in the file license.txt.
 * @package Amfphp_Examples_Authentication
 */

/**
 * includes
 */
require_once dirname(__FILE__) . '/../Includes/constants.php';
require_once dirname(__FILE__) . '/../Includes/MySQLUtil.php';
require_once dirname(__FILE__) . '/vo/Membre.php';

/**
 * Authentication and user administration service
 *
 * @package Amfphp_Examples_Authentication
 * @author Sven Dens
 */
class BTService {

    /**
     * protected methods
     * @var array 
     */
    public static $protectedMethods = array("listerEquipe");
    
    /**
     * constructor
     * @throws Exception
     */
    function __construct() {
        if (!defined('PDO::ATTR_DRIVER_NAME')) {
            throw new Exception('PDO unavailable');
        }
    }

    /**
     * sign in
     * @param string $username
     * @param string $password
     * @return boolean
     */
    public function signIn($username, $password) {
            $pdo = MySQLUtil::getConnection();

            // hash the password
            $password = DBUtils::hashPassword($password);
			
            $tsql = "SELECT * FROM membre AS m";
            $tsql .= " WHERE m.email = :email AND m.password = :pass";

            $stmt = $pdo->prepare($tsql);
			
            $stmt->bindParam(':email', $username, PDO::PARAM_STR);
            $stmt->bindParam(':pass', $password, PDO::PARAM_STR);
            $stmt->execute();

            $result = $stmt->fetch(PDO::FETCH_OBJ);
            if ($result) {
			
                AmfphpAuthentication::addRole("member");
				
                unset($result->password);
				$membre = new Membre();
				$membre->id = $result->id;
				$membre->email = $result->email;
				$membre->nom = $result->nom;
				$membre->prenom = $result->prenom;
				$membre->dateNaissance = $result->dateNaissance;
				$membre->codePostal = $result->codepostal;
				$membre->niveau = $result->niveau;
				$membre->telephone = $result->telephone;
				$membre->ville = $result->ville;
				$membre->sexe = $result->sexe;
				
                return $membre;
            } else {
                return false;
            }

    }

    /**
     * sign out function
     */
    public function signOut() {
        AmfphpAuthentication::clearSessionInfo();
		return "ok";
    }
	
	public function registerMember($membre){
		
		try {
			
			$pdo = MySQLUtil::getConnection();

            
			$tsql = "INSERT INTO membre (id, email, password, nom, prenom, telephone, codepostal, ville, sexe, niveau, dateNaissance)";
            $tsql .= " VALUES (NULL, :email, MD5(:password), :nom,:prenom,:telephone,:cp,:ville,:sexe,:niveau,:naissance);";
          
            $stmt = $pdo->prepare($tsql);
            $stmt->bindParam(':email', $membre->email, PDO::PARAM_STR);
            $stmt->bindParam(':password', $membre->password, PDO::PARAM_STR);
            $stmt->bindParam(':nom', $membre->nom, PDO::PARAM_STR);
            $stmt->bindParam(':prenom', $membre->prenom, PDO::PARAM_STR);
            $stmt->bindParam(':telephone', $membre->telephone, PDO::PARAM_STR);
            $stmt->bindParam(':cp', $membre->codePostal, PDO::PARAM_STR);
            $stmt->bindParam(':ville', $membre->ville, PDO::PARAM_STR);
            $stmt->bindParam(':sexe', $membre->sexe, PDO::PARAM_STR);
            $stmt->bindParam(':niveau', $membre->niveau, PDO::PARAM_STR);
            $stmt->bindParam(':naissance', $membre->dateNaissance, PDO::PARAM_STR);
            
			$stmt->execute();
			
        } catch (PDOException $e) {
            $error = date("Y-m-d g:i:s a T") . "\tBService::updateUser\tError: (" . $e->getCode . ") " . $e->getMessage;
            throw new Exception($error);
			return "KO";
        }
	
	
	
		return "OK";
	}
	
	
	
    /**
     * function the authentication plugin uses to get accepted roles for each function
     * Here login and logout are not protected, however
     * @param String $methodName
     * @return array
     */
    public function _getMethodRoles($methodName) {
        if (in_array($methodName, self::$protectedMethods)) {
            return array('member');
        } else {
            return null;
        }
    }

}

?>